#ifndef DEMUX_H
#define DEMUX_H

#include <string>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/avutil.h"
}
#include "Log.h"

typedef int (*interrput_callback)(void *ctx);

using namespace std;

class Demuxer
{
public:
	Demuxer(char *url = NULL);
	virtual ~Demuxer();
	void setUrl(char *url);
	int open();
	void close();
	void ffmpeg_show_error(const char *msg, int err);
	void ffmpeg_show_version(const char *module, unsigned version);
	void registerInterruptCB(interrput_callback cb, void *lpara);
	void dumpFormat();
	int readPacket(AVPacket &pkt);
	bool seek(int second);
	bool isVideoPacket(AVPacket &pkt);
	bool isAudioPacket(AVPacket &pkt);
	bool isKeyFrame(AVPacket &pkt);
	bool isRealtime();
	AVFormatContext *getContext(){return m_Ic;}
	int getVideoStream(){return m_VideoStream;}
	int getAudioStream(){return m_AudioStream;}

	typedef enum {
		UNKOWN_ERROR = -1,
		OPEN_INPUT_ERROR = -2,
		FIND_STREAM_INFO_ERROR = -3,
		OTHER_ERROR = -4,
	}DEMUX_ERROR;

public:
	string m_Url;
	AVFormatContext *m_Ic;

	/*注册的中断回调函数*/
	interrput_callback m_InterrputCB;
	void *m_CBPara;

	/*音频参数*/
	int m_AudioStream;
	int m_AudioCodec;
	int m_AudioSampleRate;
	int m_AudioChannels;
	int m_AudioChannelLayout;
	int m_AudioFormat;
	int m_AudioBitRate;
	int m_AudioFrameSize;
	AVRational m_AudioTimeBase;

	/*视频参数*/
	int m_VideoStream;
	int m_VideoCodec;
	int m_VideoHeight;
	int m_VideoWidth;
	int m_VideoFormat;
	int m_VideoBitRate;
	double m_VideoFPS;
	AVRational m_VideoTimeBase;

	int m_StreamBitRate;
	int m_Duration;
};

#endif